using Core;
using Microsoft.Xna.Framework;
using SolarWinds.Widgets;

namespace SolarWinds.Layouts
{
    public class BoxLayout : Layout
    {
        private Direction mDirection;

        public Direction Direction
        {
            get { return mDirection; }
            set { mDirection = value; }
        }	

        public BoxLayout(Direction direction)
        {
            mDirection = direction;
        }

        public BoxLayout(Direction direction, Widget parent)
            : base(parent)
        {
            mDirection = direction;
        }

        public override void Update(GameTime gameTime)
        {
            if (Parent == null) return;

            int px = Margin;
            int py = Margin + 16;
            int width = Parent.Width - 2 * Margin;

            if (mDirection == Direction.TopToBottom)
            {
                foreach (Widget w in mParent.mChildren)
                {
                    // move all items to the right position
                    w.BoundingRect = new Rectangle(px, py, width, w.Height);

                    py += w.Height + Spacing;
                }
            }
            else if (mDirection == Direction.LeftToRight)
            {
                var itemWidth = (int)((float)(width - (mParent.mChildren.Count - 1) * Spacing) / mParent.mChildren.Count);
                foreach (Widget w in Parent.mChildren)
                {
                    // move all items to the right position
                    w.BoundingRect = new Rectangle(px, py, itemWidth, 16);

                    px += itemWidth + Spacing;
                }
            }
        }
    }
}